package com.tourism.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tourism.core.common.exception.CustomException;
import com.tourism.core.common.utils.Result;
import com.tourism.domain.dto.user.AssignRoleDTO;
import com.tourism.domain.dto.user.UserPasswordDTO;
import com.tourism.domain.dto.user.UserProfileDTO;
import com.tourism.domain.dto.user.UserQueryDTO;
import com.tourism.domain.entity.User;
import com.tourism.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户控制器
 *
 * @author lucky
 */
@RequestMapping("/api/admin/user")
@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("page")
    @PreAuthorize("hasAuthority('user:page')")
    public Result<?> getPage(@RequestParam("pageIndex") Integer pageIndex,
                             @RequestParam("pageSize") Integer pageSize,
                             @RequestBody UserQueryDTO userQueryDTO) {
        return Result.success(userService.getPage(pageIndex, pageSize, userQueryDTO));
    }

    @GetMapping("get/{id}")
    @PreAuthorize("hasAuthority('user:get')")
    public Result<User> get(@PathVariable Long id) {
        return Result.success(userService.getById(id));
    }

    @PostMapping("save")
    @PreAuthorize("hasAuthority('user:save')")
    public Result<Boolean> save(@RequestBody User user) {
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<User>().eq(User::getUsername, user.getUsername());
        User u = userService.getOne(lqw);
        if (u != null) {
            throw new CustomException("用户名已存在");
        }
        return userService.save(user) ? Result.success() : Result.failure();
    }


    @PutMapping("update")
    @PreAuthorize("hasAuthority('user:update')")
    public Result<Boolean> update(@RequestBody User user) {
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<User>().eq(User::getUsername, user.getUsername());
        User u = userService.getOne(lqw);
        if (u != null) {
            throw new CustomException("用户名已存在");
        }
        return userService.updateById(user) ? Result.success() : Result.failure();
    }


    @DeleteMapping("delete")
    @PreAuthorize("hasAuthority('user:delete')")
    public Result<Boolean> delete(@RequestBody List<Long> ids) {
        if (ids.contains(1L)) {
            throw new CustomException("系统管理员不能删除");
        }
        return userService.removeBatchByIds(ids) ? Result.success() : Result.failure();
    }


    @PutMapping("update-profile")
    public Result<Boolean> updateProfile(@RequestBody UserProfileDTO userProfileDTO) {
        return userService.updateProfile(userProfileDTO) ? Result.success() : Result.failure();
    }

    @PutMapping("update-password")
    public Result<Boolean> updatePassword(@RequestBody UserPasswordDTO userPasswordDTO) {
        return userService.updatePassword(userPasswordDTO) ? Result.success() : Result.failure();
    }


    @PutMapping("assign-role")
    @PreAuthorize("hasAuthority('user:assign-role')")
    public Result<Boolean> assignRole(@RequestBody AssignRoleDTO assignRoleDTO) {
        return userService.assignRole(assignRoleDTO) ? Result.success() : Result.failure();
    }


}
